﻿using System;
using System.Collections.Generic;
using System.Text;

namespace SweetSQL.Data.Providers
{
    internal sealed class MySqlSyntaxProvider : SyntaxProviderBase
    {
        private const string PARAM = "?p";
        private const char DELIMITER = '`';

        public override bool SupportsIfExists
        {
            get { return true; }
        }

        public override string CreateParameterName(int index)
        {
            return PARAM + index;
        }

        public override void AppendTable(StringBuilder builder, TableInfo tableInfo)
        {
            if (tableInfo.Attribute.IsFullyQualified)
            {
                builder.Append(tableInfo.Name);
            }
            else
            {
                builder.Append(DELIMITER);
                builder.Append(tableInfo.Name);
                builder.Append(DELIMITER);
            }
        }

        public override void AppendColumn(StringBuilder builder, ColumnInfo columnInfo)
        {
            builder.Append(DELIMITER);
            builder.Append(columnInfo.Name);
            builder.Append(DELIMITER);
        }

        public override void AppendCreateColumn(StringBuilder builder, ColumnInfo columnInfo)
        {
            ColumnAttribute attribute = columnInfo.Attribute;
            Util.ThrowIf(string.IsNullOrEmpty(attribute.DataType), ErrorMessages.NO_DATA_TYPE, columnInfo.Name);

            //Column name
            builder.Append(DELIMITER);
            builder.Append(columnInfo.Name);
            builder.Append(DELIMITER);

            //Data type
            builder.Append(' ');
            builder.Append(attribute.DataType);

            //Size and precision
            if (attribute.Size != 0)
            {
                builder.Append("(");
                builder.Append(attribute.Size);
                if (attribute.Precision != 0)
                {
                    builder.Append(",");
                    builder.Append(attribute.Precision);
                }
                builder.Append(")");
            }

            //Nullable?
            if (!attribute.CanBeNull)
            {
                builder.Append(" NOT NULL");
            }
            else
            {
                builder.Append(" NULL");
            }
        }

        public override void AppendPrimaryKeys(StringBuilder builder, TableInfo tableInfo)
        {
            int length = tableInfo.PrimaryKeys.Count;
            if (length > 0)
            {
                builder.Append(", CONSTRAINT ");
                builder.Append(DELIMITER);
                builder.Append("PK_");
                builder.Append(tableInfo.Name);
                builder.Append(DELIMITER);
                builder.Append(" PRIMARY KEY (");
                for (int i = 0; i < length; i++)
                {
                    builder.Append(DELIMITER);
                    builder.Append(tableInfo.PrimaryKeys[i].Name);
                    builder.Append(DELIMITER);
                    if (i != length - 1)
                    {
                        builder.Append(", ");
                    }
                }
                builder.Append(")");
            }
        }
    }
}
